package com.coffee.price;

import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.coffee.price.common.BasicActivity;
import com.coffee.price.common.Const;
import com.coffee.price.common.Util;
import com.coffee.price.common.XMLParser;

public class InitApps extends BasicActivity implements OnCheckedChangeListener {
	
	private Intent intent;
	private Handler mHandler = null;
	private InitThread thread = null;
	
	private String version = "";
	private Map<String,String> notice = null;
	
	private int NETWORK_DIALOG = 1;
	//private int VERSION_DIALOG = 2;
	private int NOTICE_DIALOG = 3;
	
	private CheckBox try_view;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.init_apps);
        
        this.init();
    }
    
    public void onClick(View v) {
    }


	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if(buttonView == try_view){
			
			if(isChecked){
				Util.noticeReadWrite(this, notice.get("seq")+",Y");
			}
			else{
				Util.noticeReadWrite(this, notice.get("seq")+",N");
			}
			
		}
	}

	
	
    class InitThread implements Runnable{
		public void run() {
			// TODO Auto-generated method stub
			intent = new Intent(InitApps.this, BrandListActivity.class);
			startActivity(intent);
		}
	}
    
    public void init(){
    	
    	if(!Util.isNetworkAvailable(this)){
			
			this.ShowAlertDialog(NETWORK_DIALOG);
		}
		else{
			
			if(!Const.IS_READ){
				
				try{
					XMLParser xml = new XMLParser();
					xml.doc(xml.requestHttpUrl(Const.notice_url));
					notice = xml.notice();
					
					String notice_data = Util.noticeIsRead(this);
					String[] dateArr = notice_data.split(",");
					
					if(dateArr !=null && (!dateArr[0].equals(notice.get("seq")) || !"Y".equals(dateArr[1]))){
						this.ShowAlertDialog(NOTICE_DIALOG);
					}
					else{
						this.appStart(2000);
					}
					
					Const.IS_READ = true;
				
				} catch(Exception e){}
			}
			else{
				this.appStart(2000);
			}
		}
    }
    
    public void ShowAlertDialog(int i){
    	
    	if(i == NETWORK_DIALOG){
    		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setMessage("인터넷 연결이 불안정 합니다.")
			.setPositiveButton("확 인", new DialogInterface.OnClickListener() {
 	           public void onClick(DialogInterface dialog, int id) {
 	        	   
 	        	  finish();
 	           }
 	       	});
			dialog.setTitle("네트웍알림");
    		dialog.show();
    	}
    	else if(i == NOTICE_DIALOG){
    		
    		LayoutInflater inflater = InitApps.this.getLayoutInflater();
			View v = inflater.inflate(R.layout.notice_view, null);
			
			TextView contents = (TextView)v.findViewById(R.id.notice_msg);
			contents.setText(notice.get("contents"));
			try_view = (CheckBox)v.findViewById(R.id.try_view);
			try_view.setOnCheckedChangeListener(this);
			
    		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setView(v)
			.setPositiveButton("확 인", new DialogInterface.OnClickListener() {
 	           public void onClick(DialogInterface dialog, int id) {
 	        	  appStart(500);
 	           }
 	       	});
			dialog.setTitle("공지사항");
    		dialog.show();
    	}
    	/*else if(i == VERSION_DIALOG){
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setMessage("신규 버전이 마켓에 \n등록되었습니다.\n\n더 나은 서비스를 위해꼭 \n업데이트 받으세요!")
		.setPositiveButton("업데이트", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	        	   
	        	  Intent marketIntent = new Intent(Intent.ACTION_VIEW);
	        	  marketIntent.setData(Uri.parse("market://details?id=com.coffee.price"));
	        	  startActivity(marketIntent);
	           }
	       	})
	       	.setNeutralButton("다음에", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	        	  appStart(500); 	        	  
	           }
	       	});
		dialog.setTitle("업데이트알림");
		dialog.show();
		}*/
    }
    
    public void appStart(int delay){
    	thread = new InitThread();
        mHandler = new Handler();
        mHandler.postDelayed(thread, delay);
    }
    
    public void remove(){
    	if(thread != null)
			mHandler.removeCallbacks(thread);
    }
    
    /* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//this.init();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		this.remove();
		Const.IS_READ = false;
	}
    
    @Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		this.remove();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.remove();
	}
}